package com.steel.yfmall.cloud.assistant.impl.stock;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.steel.yfmall.cloud.assistant.api.stock.StockOperationAPI;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.bean.param.ZgProductStockParamBean;
import com.steel.yfmall.cloud.assistant.common.mapper.SteelSystemPropertiesMapper;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductSellerGoodsBean;
import com.steel.yfmall.cloud.assistant.dto.para.goods.ParaProductSellerGoodsOtherBean;
import com.steel.yfmall.cloud.assistant.dto.para.stock.ParaSteelChannelDepotSkuLimitBean;
import com.steel.yfmall.cloud.assistant.dto.para.stock.ParaSteelChannelUserGroupCodeLimit;
import com.steel.yfmall.cloud.assistant.dto.result.stock.ResultSteelChannelDepotSkuLimitBean;
import com.steel.yfmall.cloud.assistant.goods.ProductSellerGoodsService;
import com.steel.yfmall.cloud.assistant.po.common.SteelSystemProperties;
import com.steel.yfmall.cloud.assistant.po.common.SteelSystemPropertiesExample;
import com.steel.yfmall.cloud.assistant.po.goods.ProductSellerGoods;
import com.steel.yfmall.cloud.assistant.stock.SteelChannelDepotSkuBeanService;
import com.steel.yfmall.cloud.assistant.stock.SteelChannelDepotSkuLimitBeanService;
import com.steel.yfmall.cloud.assistant.stock.SteelChannelUserGroupCodeLimitService;
import com.steel.yfmall.cloud.common.api.SystemPropertiesBeanServiceAPI;
import com.steel.yfmall.cloud.common.bean.param.APIParamSystemPropertiesBean;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.Pages;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.utils.ResultCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存服务定时任务
 *
 * @author jia chang bao
 * @date 2021/2/24 14:57
 */
@Service
@Slf4j
public class StockOperationAPIImpl implements StockOperationAPI {
    /**
     * 定时任务 操作sc_channel_user_group_code_limit、和sc_channel_depot_sku_limit表，
     * 1、在库存库，找到挂牌信息表 sc_channel_depot_sku_limit 中的商品挂牌信息。 拿出字段 user_group_code 集合。对  sc_channel_user_group_code_limit 表进行条件删除。
     * 2、将库存表   sc_channel_depot_sku 中的商品库存 ，字段 last_sync_stock 改为和冻结库存一样 freeze_count。  锁定量,限购库存量字段 lock_limit_stock 改为 0
     * 3、对  sc_channel_depot_sku_limit 中的商品挂牌信息进行删除
     * 4. 将 配置设置为闭市
     *
     * @return true 成功，false失败
     * @author jia chang bao
     */
    @Override
    public APIMsgBean<Boolean> timingOperationDepotSkuAndUserGroupCode() {
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        //设置条件
        ParaProductSellerGoodsOtherBean paraProductSellerGoodsBean = new ParaProductSellerGoodsOtherBean();
        paraProductSellerGoodsBean.setStatus(1);
        paraProductSellerGoodsBean.setProdClass("H");
        log.info("定时任务timingOperationDepotSkuAndUserGroupCode启动，开始处理库存");
        //查询总数
        long count = productSellerGoodsService.count(paraProductSellerGoodsBean);
        //递归处理
        Boolean aBoolean = OperationDepotSku(paraProductSellerGoodsBean, count);
        log.info("定时任务timingOperationDepotSkuAndUserGroupCode结束");
        apiMsgBean.setData(aBoolean);
        apiMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean("操作成功"));
        return apiMsgBean;
    }

    /**
     * 递归获取 商品数据
     *
     * @param paraProductSellerGoodsBean 参数
     * @param count                      总数
     * @author jia chang bao
     */
    private Boolean OperationDepotSku(ParaProductSellerGoodsOtherBean paraProductSellerGoodsBean, long count) {
        Pages<ProductSellerGoods> productSellerGoods = productSellerGoodsService.getProductSellerGoods(paraProductSellerGoodsBean, count);
        if (productSellerGoods != null && productSellerGoods.getList() != null && productSellerGoods.getList().size() > 0) {
            List<ProductSellerGoods> list = productSellerGoods.getList();
            List<String> productSysCode = list.stream().map(ProductSellerGoods::getProductSysCode).collect(Collectors.toList());
            log.info("定时任务下架开始处理，参数：" + JSON.toJSONString(productSysCode));
            //发送队列修改为下架
            String join = Joiner.on(",").join(productSysCode);
            Map<String, String> map = new HashMap<>(AssistantConstants.DEFAULT_MAP_SIZE);
            map.put("userName", AssistantConstants.SYSTEM_USER);
            map.put("siteCode", AssistantConstants.SITE_CODE);
            map.put("productSysCodes", join);
            map.put("status", AssistantConstants.STR_YES_STR);
            //店铺商品批量上下架mq
            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
            requestSendMQBean.setSendMqTitle(AssistantConstants.STORE_GOODS_RELEASE);
            requestSendMQBean.setSendMqMessage(JSON.toJSONString(map));
            mqSendService.sendCommonMQMessage(requestSendMQBean);
        }
        //配置文件设置为闭市
        SteelSystemProperties record = new SteelSystemProperties();
        record.setPropValue("N");
        SteelSystemPropertiesExample example = new SteelSystemPropertiesExample();
        example.createCriteria().andPropKeyEqualTo("isOpen").andPropMarkEqualTo("openCloseTime");
        steelSystemPropertiesMapper.updateByExampleSelective(record,example);
        return true;
    }

    @Resource
    private ProductSellerGoodsService productSellerGoodsService;
    @Resource
    private MqSendService mqSendService;
    @Resource
    private SteelSystemPropertiesMapper steelSystemPropertiesMapper;
}
